package com.authine.cloudpivot.web.api.secondopen.controller;

import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectCreatedModel;
import com.authine.cloudpivot.engine.api.model.runtime.WorkflowInstanceModel;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.domain.FileInfo;
import com.authine.cloudpivot.web.api.secondopen.service.FileUpLoadService;
import com.authine.cloudpivot.web.api.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/fileupload")
public class FileUploadController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);

    @Autowired
    private FileUpLoadService fileUpLoadService;

    @RequestMapping("/filein")
    public AjaxResult fileUpload(String objId,String processCode){
        log.info("获取参数:objId={},processCode={}", objId, processCode);
        if (StringUtils.isBlank(objId) || StringUtils.isBlank(processCode)) {
            log.info("参数不完整，直接结束");
            return AjaxResult.error("参数不完整，直接结束");
        }
        BizObjectFacade bizObjectFacade = super.getBizObjectFacade();
        //获取业务数据
        BizObjectCreatedModel bizObject = bizObjectFacade.getBizObject(processCode, objId);
        if (bizObject == null) {
            log.info("业务对象不存在");
            return AjaxResult.error("参数不完整，直接结束");
        }
        String wId = bizObject.getWorkflowInstanceId();
        log.info("TargetOwner:wId={}", wId);
        WorkflowInstanceModel wiModel = super.getWorkflowInstanceFacade().getWorkflowInstance(wId);
        if (wiModel == null) {
            log.info("业务流程对象不存在");
        }

        //获取子表数据
        List<Map<String, Object>> bTableSheets=(List<Map<String, Object>>) bizObject.get("bzjzsmxb");
        String khmc=null;
        if (!CollectionUtils.isEmpty(bTableSheets)) {
            for (int i = 0; i < bTableSheets.size(); i++) {
                //客户名称
                khmc = String.valueOf(bTableSheets.get(i).get("khmc"));
            }
        }
        List<FileInfo> fileinfos = fileUpLoadService.getFileById(objId);
        log.info("获取文件：{}",fileinfos);
        String filename = fileinfos.get(0).getFilename();
        fileUpLoadService.copyFile(khmc,filename);
        fileUpLoadService.upFile(fileinfos,khmc);
        return AjaxResult.success();
    }

    @GetMapping("/tt")
    public AjaxResult tt(String objId,String processCode) {
        log.info("获取参数:objId={},processCode={}", objId, processCode);
        if (StringUtils.isBlank(objId) || StringUtils.isBlank(processCode)) {
            log.info("参数不完整，直接结束");
            return AjaxResult.error("参数不完整，直接结束");
        }
        BizObjectFacade bizObjectFacade = super.getBizObjectFacade();
        //获取业务数据
        BizObjectCreatedModel bizObject = bizObjectFacade.getBizObject(processCode, objId);
        if (bizObject == null) {
            log.info("业务对象不存在");
            return AjaxResult.error("参数不完整，直接结束");
        }
        String wId = bizObject.getWorkflowInstanceId();
        log.info("TargetOwner:wId={}", wId);
        WorkflowInstanceModel wiModel = super.getWorkflowInstanceFacade().getWorkflowInstance(wId);
        if (wiModel == null) {
            log.info("业务流程对象不存在");
        }

        //获取子表数据
        List<Map<String, Object>> bTableSheets=(List<Map<String, Object>>) bizObject.get("bzjzsmxb");
        String khmc=null;
        if (!CollectionUtils.isEmpty(bTableSheets)) {
            for (int i = 0; i < bTableSheets.size(); i++) {
                //客户名称
                khmc = String.valueOf(bTableSheets.get(i).get("khmc"));
            }
        }
        List<FileInfo> fileinfos = fileUpLoadService.getFileById(objId);
        log.info("获取文件：{}",fileinfos);
        String filename = fileinfos.get(0).getFilename();
        fileUpLoadService.copyFile(khmc,filename);
        fileUpLoadService.upFile(fileinfos,khmc);
        return AjaxResult.success();
    }
}
